package Math;

public class _507_PerfectNumber {

    //hint:use loop to minimize the divisor's value
    public boolean checkPerfectNumber_0(int num) {
        if (num == 0) return false;
        int sum = 0;
        int div = 1;
        int max = num;
        while (div < max) {
            if (num % div == 0) {
                max = (num / div);
                sum += div + max;
            }
            div++;
        }
        return num == (sum - num);
    }

    //reference solution:use sqrt to get the minimize divisor
    public boolean checkPerfectNumber(int num) {
        if (num == 1) return false;
        int sum = 0;
        int sqrt = (int)Math.sqrt(num);
        for (int i = 2; i <= sqrt; i++) {
            if (num % i == 0) {
                sum += i + num / i;
            }
        }
        if (sqrt * sqrt == num) {
            sum -= sqrt;
        }
        return sum + 1 == num;
    }
}
